package com.basic.business.goodsIndicators.controller;


import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.basic.business.goodsIndicators.domain.TGoodsIndicators;
import com.basic.business.goodsIndicators.dto.EditIndicatorsInfoDto;
import com.basic.business.goodsIndicators.dto.TGoodsUpNewDateDto;
import com.basic.business.goodsIndicators.service.ITGoodsIndicatorsService;
import com.basic.business.goodsIndicators.vo.CollectData;
import com.basic.business.goodsIndicators.vo.TGoodsIndicatorsVo;
import com.basic.business.pic.service.ITGoodsPicService;
import com.basic.business.upDate.domain.TGoodsUpDate;
import com.basic.business.upDate.service.ITGoodsUpDateService;
import com.basic.common.annotation.FileExport;
import com.basic.common.annotation.Log;
import com.basic.common.core.controller.BaseController;
import com.basic.common.core.domain.AjaxResult;
import com.basic.common.core.page.TableDataInfo;
import com.basic.common.enums.BusinessType;
import com.basic.common.utils.SecurityUtils;
import com.basic.common.utils.easyExcel.EasyExcelUtils;
import com.google.common.util.concurrent.RateLimiter;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 商品指标数据Controller
 *
 * @author zhaoxy_jn@163.com
 */
@RestController
@RequestMapping("/business/goodsIndicators")
public class TGoodsIndicatorsController extends BaseController {
    @Resource
    private ITGoodsIndicatorsService tGoodsIndicatorsService;

    @Resource
    private ITGoodsPicService tGoodsPicService;

    @Resource
    private ITGoodsUpDateService tGoodsUpDateService;

    // 每分钟 30 次调用
    private final RateLimiter rateLimiter = RateLimiter.create(30.0 / 60);

    /**
     * 查询商品指标数据列表
     */
    @PreAuthorize("@ss.hasPermi('business:goodsIndicators:list')")
    @GetMapping("/list")
    public TableDataInfo list(TGoodsIndicators tGoodsIndicators) {
        startPage();
        //获取商品、全站推关联查询数据
        List<TGoodsIndicatorsVo> list = null;
        if (tGoodsIndicators.getStatDateStart() == null && tGoodsIndicators.getStatDateEnd() == null) {
            list = tGoodsIndicatorsService.getListVo(tGoodsIndicators);
        } else {
            list = tGoodsIndicatorsService.getSummaryListVo(tGoodsIndicators);
        }

        for (TGoodsIndicatorsVo tGoodsIndicatorsVo : list) {
            String picUrl = tGoodsPicService.getPicUrl(tGoodsIndicatorsVo.getGoodsId());
            tGoodsIndicatorsVo.setImgUrl(picUrl);
        }
        return getDataTable(list);
    }

    /**
     * 联表查询数据列表(不分页)
     */
    @GetMapping("/allList")
    public AjaxResult allList(TGoodsIndicators tGoodsIndicators) {
        List<TGoodsIndicatorsVo> list = tGoodsIndicatorsService.getListVo(tGoodsIndicators);
        return AjaxResult.success(list);
    }

    /**
     * 获取汇总数据
     */
    @PreAuthorize("@ss.hasPermi('business:goodsIndicators:collectData')")
    @GetMapping("/getCollectData")
    public AjaxResult getCollectData(TGoodsIndicators tGoodsIndicators) {
        CollectData collectData = tGoodsIndicatorsService.getCollectData(tGoodsIndicators);
        return AjaxResult.success(collectData);
    }

    /**
     * 导出商品指标数据列表
     */
    @PreAuthorize("@ss.hasPermi('business:goodsIndicators:export')")
    @Log(title = "商品指标数据", businessType = BusinessType.EXPORT)
    @FileExport(fileName = "商品指标数据.xlsx")
    @PostMapping("/export")
    public String export(@RequestBody TGoodsIndicators tGoodsIndicators) throws IOException {
        //获取商品、全站推关联查询数据
        List<TGoodsIndicatorsVo> list = null;
        if (tGoodsIndicators.getStatDateStart() == null && tGoodsIndicators.getStatDateEnd() == null) {
            list = tGoodsIndicatorsService.getListVo(tGoodsIndicators);
        } else {
            list = tGoodsIndicatorsService.getSummaryListVo(tGoodsIndicators);
        }

        for (TGoodsIndicatorsVo tGoodsIndicatorsVo : list) {
            String picUrl = tGoodsPicService.getPicUrl(tGoodsIndicatorsVo.getGoodsId());
            tGoodsIndicatorsVo.setImgUrl(picUrl);
        }
        return EasyExcelUtils.excelExport(TGoodsIndicatorsVo.class, "商品指标数据", list);
    }

    /**
     * 获取商品指标数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:goodsIndicators:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(tGoodsIndicatorsService.get(id));
    }

    /**
     * 新增商品指标数据
     */
    @PreAuthorize("@ss.hasPermi('business:goodsIndicators:add')")
    @Log(title = "商品指标数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TGoodsIndicators tGoodsIndicators) {
        return toAjax(tGoodsIndicatorsService.insert(tGoodsIndicators));
    }

    /**
     * 修改商品指标数据
     */
    @PreAuthorize("@ss.hasPermi('business:goodsIndicators:edit')")
    @Log(title = "商品指标数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TGoodsIndicators tGoodsIndicators) {
        return toAjax(tGoodsIndicatorsService.update(tGoodsIndicators));
    }

    /**
     * 删除商品指标数据
     */
    @PreAuthorize("@ss.hasPermi('business:goodsIndicators:remove')")
    @Log(title = "商品指标数据", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody JSONObject jsonObject) {
        JSONArray jsonArray = jsonObject.getJSONArray("ids");
        String[] ids = jsonArray.toArray(String.class);
        return toAjax(tGoodsIndicatorsService.deleteByIdsNew(ids));
    }

    /**
     * 上传商品指标数据
     *
     * @param file
     * @return
     */
    @PostMapping("/uploadData")
    public AjaxResult uploadData(MultipartFile file, String goodsStoreId, Date goodsSelectDate, String goodsFileHash) {
        if (!rateLimiter.tryAcquire()) {
            return AjaxResult.error("请求过多，请稍后再试!");
        } else {
            tGoodsIndicatorsService.uploadIndicatorsData(file, goodsSelectDate, goodsStoreId, goodsFileHash);
            return AjaxResult.success();
        }
    }

    /**
     * 导出无上新日期商品数据
     */
    @FileExport(fileName = "无上新日期商品数据.xlsx")
    @PostMapping("/exportNoUpNewDate")
    public String exportNoUpNewDate() throws IOException {
        Long deptId = SecurityUtils.getDeptId();
        List<TGoodsUpNewDateDto> list = tGoodsIndicatorsService.exportNoUpNewDate(deptId);
        return EasyExcelUtils.excelExport(TGoodsUpNewDateDto.class, "无上新日期商品数据", list);
    }

    /**
     * 获取商品指标上新日期和商品标签信息
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/getIndicatorsInfo/{id}")
    public AjaxResult getIndicatorsInfo(@PathVariable("id") String id) {
        EditIndicatorsInfoDto dto = new EditIndicatorsInfoDto();

        TGoodsIndicators tGoodsIndicators = tGoodsIndicatorsService.get(id);
        dto.setId(id);
        dto.setGoodsId(tGoodsIndicators.getGoodsId());
        dto.setGoodsLabel(tGoodsIndicators.getGoodsLabel());

        TGoodsUpDate tGoodsUpDate = tGoodsUpDateService.get(tGoodsIndicators.getGoodsId());
        if (tGoodsUpDate != null) {
            dto.setUpNewDate(tGoodsUpDate.getUpNewDate());
        }

        return success(dto);
    }

    /**
     * 修改商品指标数据
     */
    @PutMapping("/editIndicatorsInfo")
    public AjaxResult editIndicatorsInfo(@RequestBody EditIndicatorsInfoDto infoDto) {
        //修改商品标签
        tGoodsIndicatorsService.updateLabelByGoodsId(infoDto.getGoodsLabel(), infoDto.getGoodsId());
        //修改商品上新日期
        tGoodsUpDateService.updateDateByGoodsId(infoDto.getUpNewDate(), infoDto.getGoodsId());
        return toAjax(true);
    }


    /**
     * 定时任务修改商品标签指标数据
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void scheduledUpdateGoodsLabel() {
        tGoodsIndicatorsService.scheduledUpdateGoodsLabel();
    }
}
